<!DOCTYPE html>
<html>
<head>
    <title>归还设备</title>
    {% include 'system/common/header.html' %}</head>
<body class="pear-container">
<div class="layui-card">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">设备ID</label>
            <div class="layui-input-block">
                <input type="text" name="deviceId" autocomplete="off" placeholder="请输入" lay-verify="required"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">借用时间</label>
            <div class="layui-input-block">
                <input type="text" name="borrow_at" autocomplete="off" placeholder="请输入" lay-verify="required"
                       class="layui-input" id="time">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="submit">确认</button>
        </div>
    </form>
</div>
</body>


{% include 'system/common/footer.html' %}
<script>
    layui.use(['form', 'table'], function () {
        let form = layui.form
        let laydate = layui.laydate;
        let $ = layui.$;
        let MODULE_PATH = "{{ url_for('system.device.main') }}"

        laydate.render({
            elem: '#time',
            max: 0
        });

        form.on('submit(submit)', function (data) {
            event.preventDefault();
            let cData = JSON.stringify(data.field);
            $.ajax({
                url: MODULE_PATH + 'back_device',
                data: cData,
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    console.log(result)

                    if (result.success) {
                        /*layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            location.reload();
                        })*/
                        window.success(result)

                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
        })

        window.success = function (obj) {
        if (obj.data.pay_money > 0) {
            layer.alert('请支付' + obj.data.pay_money + '元', {
                btn: ['去支付', '取消'],
                title: '提示',
                btn1: function (index) {
                    $.ajax({
                        url: MODULE_PATH + 'back/pay',
                        data: JSON.stringify({
                            'id': obj.data.id,
                            'pay_money': obj.data.pay_money,
                            'deviceId': obj.data.deviceId,
                        }),
                        dataType: 'json',
                        contentType: 'application/json',
                        type: 'put',
                        success: function (result) {
                            console.log(result)
                            if (result.success) {
                                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                    location.reload();
                                })
                            } else {
                                layer.msg(result.msg, {icon: 2, time: 1000});
                            }
                        }
                    })
                },
                btn2: function (index) {
                    layer.msg('取消支付，归还失败', {icon: 2, time: 1000})
                    layer.close(index)
                }
            });
        } else {
            layer.msg(obj.msg, {icon: 1, time: 1000}, function () {
                location.reload();
            })
        }


    }
    });
</script>
</html>